<?php

class ManageGuestbookController extends Controller {
	var $name = 'ManageGuestbook';
    var $plugin_uses = array('Guestbook');
    var $no_session = array('add_guestbook');

	function index() {
		$this->action = 'manage_guestbook';
		$this->manage_guestbook();
	}

	function manage_guestbook($page = 0) {
	    $page = (int)$page;
        $results = get_all_guestbook($page, 'all', admin_url("/manage_guestbook/manage_guestbook"));
        $this->set('results', $results);
        $this->set('this_url', $this->__this_url());
	}

    function show_guestbook($id = 0, $status = null) {
        if (empty($this->referer)) {
            $redirect = '/manage_guestbook';
        } else {
            $redirect = $this->referer;
        }
        if (empty($id)) {
            $this->redirect($redirect);
        }
        $post = $this->Guestbook->read('', $id);
        if (empty($post)) {
            $this->redirect($redirect);
        }
        $reply = $this->Guestbook->find("parent='$id'");
        if (empty($reply)) {
            $content = '';
            $reply_id = 0;
        } else {
            $content = $reply->content;
            $reply_id = $reply->id;
        }
        if (!empty($this->data['Guestbook'])) {
            if (!empty($this->data['Guestbook']['shenhe'])) {
                $this->data['Guestbook']['status'] = 1;
            } else {
                $this->data['Guestbook']['status'] = 0;
            }
            $this->data['Guestbook']['id'] = $id;
            $this->Guestbook->save($this->data);
            $data['Guestbook']['content'] = sanitize_title($this->data['Guestbook']['reply']);
            $data['Guestbook']['id'] = $reply_id;
            $data['Guestbook']['parent'] = $id;
            $data['Guestbook']['post_time'] = time();
            $user = current_user();
            $data['Guestbook']['author'] = $user->username;
            $this->Guestbook->save($data);
            $this->redirect('/manage_guestbook/show_guestbook/' . $id . '/done?' . $redirect);
        }

        if ($status == 'done') {
            $this->header_notice = '提交成功!';
        }
        $this->data['Guestbook']['reply'] = $content;
        $this->data['Guestbook']['shenhe'] = $post->status;
        $this->set('post', $post);
        $this->set('referer', $redirect);
    }

    function add_guestbook() {
        $this->layout = null;
        if (!empty($this->data) && !empty($this->referer)) {
            $this->data['Guestbook']['title'] = isset($this->data['Guestbook']['title']) ? sanitize_title($this->data['Guestbook']['title']) : '';
            $this->data['Guestbook']['author'] = isset($this->data['Guestbook']['author']) ? sanitize_title($this->data['Guestbook']['author']) : '';
            $this->data['Guestbook']['author_contact'] = isset($this->data['Guestbook']['author_contact']) ? sanitize_title($this->data['Guestbook']['author_contact']) : '';
            $this->data['Guestbook']['content'] = isset($this->data['Guestbook']['content']) ? sanitize_title($this->data['Guestbook']['content']) : '';
            $this->data['Guestbook']['post_time'] = time();
            $this->data['Guestbook']['status'] = 0;

            $this->data['Guestbook']['author_ip'] = preg_replace('/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR']);
            $this->data['Guestbook']['author_agent'] = $_SERVER['HTTP_USER_AGENT'];
            $this->data['Guestbook']['parent'] = 0;
            if (empty($this->data['Guestbook']['title'])) {
                die('没有填写留言标题,请返回填写.');
            }
            if (empty($this->data['Guestbook']['content'])) {
                die('没有填写留言内容,请返回填写.');
            }
            $this->Guestbook->save($this->data);
            al_redirect($this->referer);
        } else if (empty($this->referer)) {
            die('没有设置返回地址,请设置.');
        }
    }

    function delete($id = null) {
        if (empty($id)) {
            $this->error_404();
        }
        $this->Guestbook->del($id);
        $this->Guestbook->del_all("`parent`='$id'");
        $this->redirect($this->referer);
    }

    function __this_url() {
        $posts_url = str_replace('delete_done', '', $this->url());
        $posts_url = rtrim($posts_url, '/');
        if ($posts_url == '/manage_guestbook') {
            $posts_url = '/manage_guestbook/manage_guestbook';
        }
        return $posts_url;
    }
}

?>
